// uecgi_http.h
#ifndef UE_CGI_HTTP_H
#define UE_CGI_HTTP_H

#include <map>
#include <string>

// HTTP request parameter name definitions
#define PARAMETER_PASSWORD_NAME        "password"
#define PARAMETER_SESSIONID_NAME       "sessionid"
#define PARAMETER_TOKEN_NAME           "token"
#define PARAMETER_UEID_NAME            "ueid"
#define PARAMETER_ACTION_NAME          "action"
#define PARAMETER_TARGET_X_NAME        "x"
#define PARAMETER_TARGET_Y_NAME        "y"

class CUECGIHttp
{
public:
	CUECGIHttp();
	~CUECGIHttp();

public:
	/* Parse query list from the query string
	 * @param[in] strQueries The query string to parse
	 * @return TRUE indicate success, otherwise some error occur.
	 */
	BOOL ParseQueries(char* strQueries);

	/* Add a new item into query list
	 * @param[in] strQuery The new query to be added. Please be noted that the content of this string possiblly be modified during parse.
	 * @return TRUE indicate success, otherwise some error occur.
	 */
	BOOL AddQuery(char* strQuery);

	/* Get the value of a query parameter by the name
	 * @param[in] strQueryName The name of the query parameter.
	 * @return NULL indicate the specified query parameter can't be find, otherwise return pointer of the NULL-terminated string for the value of the query parameter
	 */
	const char* GetQuery(const char* strQueryName);

	/* Parse cookie list from the cookies string
	 * @param[in] strCookies The cookies string to parse. Please be noted that the content of this string possiblly be modified during parse.
	 * @return TRUE indicate success, otherwise some error occur.
	 */
	BOOL ParseCookies(char* strCookies);

	/* Add a new item into cookie list
	 * @param[in] strCookie The new cookie to be added. Please be noted that the content of this string possiblly be modified during parse.
	 * @return TRUE indicate success, otherwise some error occur.
	 */
	BOOL AddCookie(char* strCookie);

	/* Clear all the cookies.
	 * @return None.
	 */
	void ClearCookies();

	/* Clear http body and free related memory
	 * @return None.
	 */
	void ClearBody();

	/* Get cookies string from environment variables and parse it
	 * @return TRUE indicate success, otherwise some error occur.
	 */
	BOOL RetrieveCookies();

	/* Retrieve http body from stdin
	 * @return TRUE indicate success, otherwise some error occur.
	 */
	BOOL RetrieveBody();

	/* Get the value of a cookie by the name
	 * @param[in] strCookieName The name of the cookie.
	 * @return NULL indicate the specified cookie can't be find, otherwise return pointer of the NULL-terminated string for the value of the cookie
	 */
	const char* GetCookie(const char* strCookieName);

	/* Get the session id from request
	 * @return NULL indicate session id can't be find, otherwise return pointer of the NULL-terminated string for the value of the session id
	 */
	const char* GetSessionID();

	/* Get HTTP body length.
	 * @return 0 indicate the request has no body or retrieve body failed. Otherwise return the body length.
	 */
	int GetBodyLength();

	/* Get HTTP body data.
	 * @return NULL indicate the request has no body or retrieve body failed. Otherwise return the point to the body.
	 */
	void* GetBody();

protected:
	std::map<std::string, std::string> m_Cookies;        // store all cookies
	BOOL                               m_bCookieParsed;  // indicate whether m_Cookies is valid
	std::map<std::string, std::string> m_Queries;
	void*                              m_pBody;          // http body
	int                                m_nBodyLen;       // length of http body
	BOOL                               m_bBodyRetrieved; // indicate whether m_nBodyLen and m_pBody are valid
};

#endif // UE_CGI_HTTP_H